package com.ztesoft.web.common.db.dao;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.ztesoft.core.common.Page;
import com.ztesoft.framework.exception.SysRuntimeException;
import com.ztesoft.framework.util.StringUtils;
import com.ztesoft.web.common.db.arg.SysModuleArg;
import com.ztesoft.web.common.db.arg.SysModuleArg.SysModuleCriteria;
import com.ztesoft.web.common.db.dao.mapper.ISysModuleMapper;
import com.ztesoft.web.common.db.po.SysModuleNodePO;
import com.ztesoft.web.common.db.po.SysModulePO;

@Repository
public class SysModuleDao extends SqlSessionDaoSupport {

	@Resource(name = "majorSqlSessionTemplate")
	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		super.setSqlSessionTemplate(sqlSessionTemplate);
	}

	public List<Integer> countByArg(SysModuleArg arg) {
		return getMapper().countByArg(arg);
	}
	//系统模块维护 删除
	public int deleteByArg(SysModuleArg arg) {
		return getMapper().deleteByArg(arg);
	}

	public List<SysModulePO> selectByArg(SysModuleArg arg) {
		return getMapper().selectByArg(arg);
	}

	public List<SysModuleNodePO> queryAllForTree() {
		return getMapper().queryAllForTree();
	}

	public int updateByArgSelective(SysModulePO record, SysModuleArg arg) {
		return getMapper().updateByArgSelective(record, arg);
	}
	 //系统模块维护 修改子集level
	public int updateByArg(SysModulePO record, SysModuleArg arg) {
		return getMapper().updateByArg(record, arg);
	}

	public Page<SysModulePO> selectByArgAndPage(SysModuleArg arg, Page<SysModulePO> resultPage) {
		List<SysModulePO> resultList = getMapper().selectByArgAndPage(arg, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}
	//系统模块维护查询
	public Page<SysModulePO> selectByArgAndPageSys(SysModuleArg arg, Page<SysModulePO> resultPage) {
		List<SysModulePO> resultList = getMapper().selectByArgAndPageSys(arg, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}

	public int insert(SysModulePO record) {
		return getMapper().insert(record);
	}
	//系统模块维护 添加
	public int insertSelective(SysModulePO record) {
		return getMapper().insertSelective(record);
	}

	public int insertBatch(List<SysModulePO> records) {
		return getMapper().insertBatch(records);
	}
	//系统模块维护
	public int deleteByPrimaryKey(Integer key) {
		return getMapper().deleteByPrimaryKey(key);
	}

	public SysModulePO selectByPrimaryKey(Integer key) {
		return getMapper().selectByPrimaryKey(key);
	}
	//系统模块维护 修改
	public int updateByPrimaryKeySelective(SysModulePO record) {
		return getMapper().updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKey(SysModulePO record) {
		return getMapper().updateByPrimaryKey(record);
	}

	/**
	 * 根据传入的Map条件进行查询，当前仅支持所有Map中Key字段的EqualTo查询
	 * 
	 * @param params
	 *            Map,Key=字段名，value=查询值
	 * @return
	 */
	public List<SysModulePO> selectByMap(Map<String, Object> params) {
		return (selectByArg(buildQueryObject(params)));
	}

	private SysModuleArg buildQueryObject(Map<String, Object> params) {

		SysModuleArg arg = new SysModuleArg();
		SysModuleCriteria criteria = arg.createCriteria();

		Class criteriaClass = criteria.getClass();
		Set keys = params.keySet();

		if (keys != null) {

			Iterator iterator = keys.iterator();

			while (iterator.hasNext()) {

				Object key = iterator.next();
				Object value = params.get(key);
				for (Method method : criteriaClass.getMethods()) {
					if (method.getName().equals("and" + StringUtils.toUpperCaseFirstOne(key.toString()) + "EqualTo")) {
						try {
							method.invoke(criteria, value);
						} catch (Exception e) {
							throw new SysRuntimeException(e);
						}
						break;
					}
				}
			}
		}
		return arg;
	}

	public ISysModuleMapper getMapper() {
		return getSqlSession().getMapper(ISysModuleMapper.class);
	}
	//系统模块维护 是否有相同的模块名
	public List<SysModulePO> selectByModuleName(Integer parentModuleId) {
		// TODO Auto-generated method stub
		return getMapper().selectByModuleName(parentModuleId);
	}
	//系统模块维护 返回上级模块级别
	public SysModulePO selectByParentModuleId(Integer parentModuleId) {
		// TODO Auto-generated method stub
		return getMapper().selectByParentModuleId(parentModuleId);
	}
	//系统模块维护 需要修改或删除子节点的查询
	public List<SysModulePO> selectByParentModuleIdTree(SysModuleArg arg) {
		// TODO Auto-generated method stub
		return getMapper().selectByParentModuleIdTree(arg);
	}
	//系统模块维护 返回当前模块级别
	public SysModulePO selectById(Integer moduleId) {
		// TODO Auto-generated method stub
		return getMapper().selectById(moduleId);
	}
}
